package com.org.es5.api.entity;

import com.org.platform.utils.CommonUtil;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description
 * @auther dh
 * @date 2021/12/28 8:50
 */
@Setter
@Getter
public class MarketItemReq {

	private Long itemStoreId = null;
	private Long brandClassifyId = null;
	private Long saleClassifyId = null;
	private List<String> tagList = null;

	private List<Long> activityMainIds = new ArrayList<>();

	public MarketItemReq(Long itemStoreId, Long brandClassifyId, Long saleClassifyId, List<String> tagList) {
		this.itemStoreId = itemStoreId;
		this.brandClassifyId = brandClassifyId;
		this.saleClassifyId = saleClassifyId;
		this.tagList = tagList;
	}

	private List<String> getItemKey() {
		List<String> resultList = new ArrayList<>();
		if (!CommonUtil.isNullOrEmpty(itemStoreId)) {
			resultList.add("item_store_id:w:" + itemStoreId);
			resultList.add("item_store_id:b:" + itemStoreId);
		}
		if (!CommonUtil.isNullOrEmpty(brandClassifyId)) {
			resultList.add("brand_classify_id:w:" + brandClassifyId);
			resultList.add("brand_classify_id:b:" + brandClassifyId);
		}
		if (!CommonUtil.isNullOrEmpty(saleClassifyId)) {
			resultList.add("sale_classify_id:w:" + saleClassifyId);
			resultList.add("sale_classify_id:b:" + saleClassifyId);
		}
		if (null != tagList && tagList.size() > 0) {
			for (String tagId : tagList) {
				resultList.add("tag_id:w:" + tagId);
				resultList.add("tag_id:b:" + tagId);
			}
		}
		return resultList;
	}

	public void fillActivity(List<MarketItem> marketItemList) {
		if (marketItemList == null || marketItemList.size() <= 0) {
			return;
		}
		List<String> itemKey = getItemKey();
		List<MarketItem> marketWhiteItemList = marketItemList.stream().filter(e -> CommonUtil.equals(e.getMerBlackWhiteType(), "w")).collect(Collectors.toList());
		List<MarketItem> marketBlackItemList = marketItemList.stream().filter(e -> CommonUtil.equals(e.getMerBlackWhiteType(), "b")).collect(Collectors.toList());
		for (MarketItem marketItem : marketWhiteItemList) {
			Long activityMainId = marketItem.getActivityMainId();
			String compareKey = marketItem.getSelectType() + ":" + marketItem.getMerBlackWhiteType() + ":" + marketItem.getSelectId();
			if (itemKey.contains(compareKey) && !activityMainIds.contains(activityMainId)) {
				activityMainIds.add(activityMainId);
			}
		}
		for (MarketItem marketItem : marketBlackItemList) {
			Long activityMainId = marketItem.getActivityMainId();
			if (!activityMainIds.contains(activityMainId)) {
				activityMainIds.add(activityMainId);
			}
		}
		for (MarketItem marketItem : marketBlackItemList) {
			Long activityMainId = marketItem.getActivityMainId();
			String compareKey = marketItem.getSelectType() + ":" + marketItem.getMerBlackWhiteType() + ":" + marketItem.getSelectId();
			if (itemKey.contains(compareKey) && activityMainIds.contains(activityMainId)) {
				activityMainIds.remove(activityMainId);
			}
		}
	}

}
